SetGfxDriver 1:Graphics 1024,768,8,2:SeedRnd MilliSecs() :SetBuffer FrontBuffer() 
Global zzz1,z,nums,facts,fact,factz,factw,resort,resortw,resortnums,unittestcnt,unittestnums,unittestfact,unittestfacts,unittestfactz,null1
nums = CreateBank(37):facts = CreateBank(37):fact = CreateBank(1117):factz= CreateBank(4000):factw= CreateBank(37)
resort = CreateBank(20000):resortw = CreateBank(20000):resortnums= CreateBank(37):unittestcnt=0
unittestnums= CreateBank(37):unittestfact= CreateBank(1117):unittestfacts= CreateBank(37):unittestfactz=CreateBank(4000)
Dim decode(1000000)
Dim encode(1000000)
		Dim aaa(3700)
Function randnum()
For i = 0 To 36 
t = Rand(1,255):PokeByte nums,i, t:PokeByte fact,i, t
Next 
End Function 
decodet()
Dim zzz(10000)


While Not KeyHit(1) 
ClsColor 0,0,0
Cls
If (unittestcnt<1 )Then

randnum()

downfacts()

factstofact()

facttofactw()
;getfactw()
;resortfactz()
EndIf
getnums()
getfacts()
getfact()
resortfactz()

unittest()


;If KeyHit(57) > 0 
If (unittestcnt<1 )Then
;WaitMouse()
EndIf
For i=0 To zzz1
	Color 255,255,0:Text i*13,300,Str(zzz(i))+",",False ,False 
Next
	Color 255,255,0:Text 13,320,(55-120) Mod 120,False ,False 
	Flip 
Wend 
Function mod1(xx)
If (xx<0) Then Return (121+xx) Else Return xx
End Function
Function mod2(xx)
If (xx<0) Then Return (256+xx) Else Return xx
End Function
Function mod3(xx)
If (xx>255) Then Return (xx-256) Else Return xx
End Function


	Function unittest()
	For i=0 To 36
	PokeByte unittestnums,i,mod2(PeekByte (resortnums,i)-unittestcnt )
	PokeByte unittestfact,i,mod2(PeekByte (resortnums,i)-unittestcnt )
	Next
	For y=0 To 4
	For x=0 To 4
	Color 255,255,0:Text 360+ x*32,y*9+9,PeekByte (unittestnums,y*5+x),False ,False 
	Next
	Next



	For y=0 To 4
	For x=5 To 1 Step -1
	maxx =0
	For xt=0 To 4
	xx = PeekByte (unittestfact,y*5+xt):If maxx<xx Then maxx=xx:yy2=y*5+xt
	Next 
	PokeByte unittestfact,yy2,0:PokeByte unittestfacts,yy2,x
	Next
	Next


	For y=0 To 4
	For x=0 To 4
	Color 0,255,0:Text 360+x*32,60+y*9+9,PeekByte (unittestfacts,y*5+x),False ,False 
	Next
	Next

	For y=0 To 4
	ttt =Int( PeekByte (unittestfacts,y*5+0) +""+PeekByte (unittestfacts,y*5+1) +""+PeekByte (unittestfacts,y*5+2)+""+PeekByte (unittestfacts,y*5+3)+""+PeekByte (unittestfacts,y*5+4))
	xx = encode(ttt*5)
	PokeInt unittestfactz,y*10 ,mod1(xx- unittestcnt )
	PokeInt unittestfactz,60+y*10,mod1(xx- unittestcnt )
	Next
	

		For xt=0 To 4
			aaa(60+xt*10 )= PeekByte (unittestfactz,60+xt*10)+1
		Next
				
		For x=5 To 1 Step -1
		maxx =0
		For xt=0 To 4
		xx = aaa(60+xt*10):If maxx<xx Then maxx=xx:yy2=60+xt*10
;		xx = PeekByte (unittestfactz,60+xt*10):If maxx<xx Then maxx=xx:yy2=60+xt*10
		Next 
		aaa(yy2) =-10:PokeByte unittestfactz,60+yy2,x
;		PokeByte unittestfactz,yy2,0:PokeByte unittestfactz,60+yy2,x
		Next
	
	ttt =Int( PeekByte (unittestfactz,120+0) +""+PeekByte (unittestfactz,120+10) +""+PeekByte (unittestfactz,120+20)+""+PeekByte (unittestfactz,120+30)+""+PeekByte (unittestfactz,120+40))
	xx = encode(ttt*5):PokeByte unittestfactz,56,xx


	For y=0 To 4
	Color 0,255,0:Text 360+x*32,120+y*9+9,PeekInt (unittestfactz,y*10),False ,False 
	Color 255,0,0:Text 360+32+x*32,120+y*9+9,PeekByte (unittestfactz,120+y*10),False ,False
	Next
	Color 255,255,0:Text 360+44+x*32,120+2*9+9,PeekInt (unittestfactz,56),False ,False
	Color 255,0,0:Text 35+360+44+x*32,120+2*9+9,null1,False ,False
	If (PeekInt (unittestfactz,56)=0) null1=null1+1:Flip:zzz(zzz1)=zzz(zzz1)+1;WaitMouse()
	
	unittestcnt = unittestcnt +1;PeekInt (factz,56)
	If(unittestcnt>121) unittestcnt =0:null1=0:zzz1=zzz1+1
;	If(unittestcnt>0) unittestcnt = PeekInt (factz,56)
	End Function


Function resortfactz()
ttt$ = decode(0)
x1=Mid$(ttt$,1,1)
x2=Mid$(ttt$,2,1)
x3=Mid$(ttt$,3,1)
x4=Mid$(ttt$,4,1)
x5=Mid$(ttt$,5,1)
PokeByte resort,0,x1
PokeByte resort,1,x2
PokeByte resort,2,x3
PokeByte resort,3,x4
PokeByte resort,4,x5

For y=0 To 4
Color 255,255,0:Text 64,120+y*9+9,PeekByte (resort,y),False ,False 
Next

xx2=0
For y1=0 To 4
xx=PeekByte (resort,y1)
For y=0 To 4
If(PeekByte (factz,120+y*10) = xx) xx3=PeekByte (factz,y*10)
Next
PokeByte resort,xx2,xx3:xx2=xx2+5
Next

For y=0 To 4
PokeByte resort,25+y*5,(PeekByte (resort,0+y*5) + PeekByte (factz,56)) Mod 120
Color 255,255,0:Text 93+x*32,120+y*9+9,PeekByte (resort,0+y*5),False ,False 
Color 255,255,0:Text 128+x*32,120+y*9+9,PeekByte (resort,25+y*5),False ,False
Next

	For y=0 To 4
	ttt$ = decode(PeekInt (resort,25+y*5)*5)
	x1=Mid$(ttt$,1,1)
	x2=Mid$(ttt$,2,1)
	x3=Mid$(ttt$,3,1)
	x4=Mid$(ttt$,4,1)
	x5=Mid$(ttt$,5,1)
	PokeByte resortw,y*5+0,x1
	PokeByte resortw,y*5+1,x2
	PokeByte resortw,y*5+2,x3
	PokeByte resortw,y*5+3,x4
	PokeByte resortw,y*5+4,x5
	Next

	For y=0 To 4
	For x=0 To 4
	Color 255,255,0:Text 160+x*32,60+y*9+9,PeekByte (resortw,y*5+x),False ,False 
	Next
	Next
	
xx2=0
For x=0 To 4
For y1=0 To 4
xx=PeekByte (resortw,x*5+y1)
For y=0 To 4
If(PeekByte (facts,x*5+y) = xx) xx3=mod3(PeekInt (nums,x*5+y) + PeekInt (factz,56))
Next
PokeByte resortnums,xx2,xx3:xx2=xx2+1
Next
Next

	For y=0 To 4
	For x=0 To 4
	Color 255,255,0:Text 160+ x*32,y*9+9,PeekByte (resortnums,y*5+x),False ,False 
	Next
	Next	
	
	
End Function


	Function facttofactw()
	For y=0 To 4
	ttt$ = decode(PeekInt (factz,y*10)*5)
	x1=Mid$(ttt$,1,1)
	x2=Mid$(ttt$,2,1)
	x3=Mid$(ttt$,3,1)
	x4=Mid$(ttt$,4,1)
	x5=Mid$(ttt$,5,1)
	PokeByte factw,y*5+0,x1
	PokeByte factw,y*5+1,x2
	PokeByte factw,y*5+2,x3
	PokeByte factw,y*5+3,x4
	PokeByte factw,y*5+4,x5
	Next
	End Function

Function factstofact()
For y=0 To 4
ttt =Int( PeekByte (facts,y*5+0) +""+PeekByte (facts,y*5+1) +""+PeekByte (facts,y*5+2)+""+PeekByte (facts,y*5+3)+""+PeekByte (facts,y*5+4))
xx = encode(ttt*5)
PokeInt factz,y*10,xx
PokeInt factz,60+y*10,xx
Next


	For xt=0 To 4
		aaa(60+xt*10 )=PeekByte (factz,60+xt*10)+1
	Next


	For x=5 To 1 Step -1
	maxx =0
	For xt=0 To 4
	xx = aaa(60+xt*10):If maxx<xx Then maxx=xx:yy2=60+xt*10
	Next 
	aaa(yy2) =-10:PokeByte factz,60+yy2,x
	Next

ttt =Int( PeekByte (factz,120+0) +""+PeekByte (factz,120+10) +""+PeekByte (factz,120+20)+""+PeekByte (factz,120+30)+""+PeekByte (factz,120+40))
xx = encode(ttt*5):PokeByte factz,56,xx

End Function

	Function downfacts()
	For y=0 To 4	
		For xt=0 To 4
			aaa(y*5+xt )=PeekByte (fact,y*5+xt)+1
		Next
	Next	

	For y=0 To 4
	For x=5 To 1 Step -1
	maxx =0
	For xt=0 To 4
	xx = aaa(y*5+xt):If maxx<xx Then maxx=xx:yy2=y*5+xt
;	xx = PeekByte (fact,y*5+xt):If maxx<xx Then maxx=xx:yy2=y*5+xt	
	Next 
	aaa(yy2) =-10:PokeByte facts,yy2,x
;	PokeByte fact,yy2,0:PokeByte facts,yy2,x	

	Next
	Next
	End Function

Function getfacts()
For y=0 To 4
For x=0 To 4
Color 0,255,0:Text x*32,60+y*9+9,PeekByte (facts,y*5+x),False ,False 
Next
Next
End Function

	Function getnums()
	For y=0 To 4
	For x=0 To 4
	Color 0,255,0:Text x*32,y*9+9,PeekByte (nums,y*5+x),False ,False 
	Next
	Next
	End Function
	
Function getfact()
For y=0 To 4
Color 0,255,0:Text x*32,120+y*9+9,PeekInt (factz,y*10),False ,False 
Color 255,0,0:Text 32+x*32,120+y*9+9,PeekInt (factz,120+y*10),False ,False
Next
Color 255,255,0:Text 44+x*32,120+2*9+9,PeekInt (factz,56),False ,False
End Function

	Function getfactw()
	For y=0 To 4
	For x=0 To 4
	Color 0,255,0:Text 160+x*32,60+y*9+9,PeekByte (factw,y*5+x),False ,False 
	Next
	Next
	End Function

Function decodet()
y=0
For x1=1 To 5
For x2=1 To 5
For x3=1 To 5
For x4=1 To 5
For x5=1 To 5
If((x1 <> x2) And (x1<>x3) And (x1<>x4) And (x1<>x5) And (x2<>x3) And (x2<>x4) And (x2<>x5) And (x3<>x4) And (x3<>x5) And (x4<>x5)) decode(y*5) =x1+""+x2+""+x3+""+x4+""+x5:encode(Int(x1+""+x2+""+x3+""+x4+""+x5)*5)=y:y=y+1
Next
Next
Next
Next
Next
;For x5=0 To 5
;Color 0,255,0:Text 500,x5*9,encode(x5*2),False ,False 
;Next
End Function